package com.huang.eduorder.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huang.eduorder.entity.Order;
import com.huang.eduorder.service.OrderService;

import com.huang.servicebase.execptionhandler.huangException;
import com.hunag.utils.JwtUtils;
import com.hunag.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;


/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-04-03
 */
@RestController
@RequestMapping("/eduorder/order")
public class OrderController {

    @Autowired
    private OrderService service;


    @PostMapping("createOrder/{courseId}")
    public R createOrder(@PathVariable String courseId,HttpServletRequest request) {
       String id = JwtUtils.getMemberIdByJwtToken(request);
        System.out.println(id);
       if(StringUtils.isEmpty(id)){
           throw new huangException(20001,"获取不到id");
       }
        String orderNo=service.createOrder(courseId,id);
        return R.ok().data("orderNo",orderNo);
    }

    //根据订单Id获取订单的信息
    @GetMapping("getOrderInfo/{id}")
    public R getOrderInfo(@PathVariable String id){
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("order_no",id);
        Order order = service.getOne(wrapper);
        return  R.ok().data("item",order);
    }
//查询是否购买
    @GetMapping("isBuy/{memberId}/{courseId}")
    public  boolean isBuy(@PathVariable String memberId,@PathVariable String courseId){
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("member_id",memberId);
        wrapper.eq("courseId",courseId);
        wrapper.eq("status",1);
       Integer count = service.count(wrapper);
        if(count>0) {
            return true;
        }
return false;

    }

}

